import pygame,sys
from pygame.locals import *
from defines import *

class hero:
    def __init__(self,screen):
        
        self.screen = screen
        self.StartLocation = [300,550]
        self.JumpCount = 0
        self.Jumped = False
        self.moveLeft = False
        self.moveRight = False
        self.moveUp = False
        self.moveDown = False
        self.PlayerImage = []
        self.MoveImageCount = 0 #counts which of the moving images is curently used
        self.ImageCount = 0
        self.CurrentHeroImage = pygame.image.load('mario_data/mario1.png')
        self.CurrentHeroImage = pygame.transform.scale(self.CurrentHeroImage, (self.CurrentHeroImage.get_width()*2,self.CurrentHeroImage.get_width()*2))
        self.SetupHeroImage()        
        
   
    def SetupHeroImage(self):
        for i in range(5):
            file = 'mario_data/mario%d.png' % (i+1)    
            self.PlayerImage.append(pygame.image.load(file)) 
            #resize image, as its too small
            self.PlayerImage[i] = pygame.transform.scale(self.PlayerImage[i], (self.PlayerImage[i].get_width()*2,self.PlayerImage[i].get_width()*2))
    
        self.PlayerRect = self.PlayerImage[0].get_rect()
        self.PlayerRect.topleft = self.StartLocation
        #self.UpdateHeroImageOnScreen()
    
    def GetEvent(self):
        # check for the QUIT event
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                # change the keyboard variables
                if event.key == K_LEFT or event.key == ord('a'):
                    self.moveRight = False
                    self.moveLeft = True
                if event.key == K_RIGHT or event.key == ord('d'):
                    self.moveLeft = False
                    self.moveRight = True
                if event.key == K_UP or event.key == ord('w'):
                    self.moveDown = False
                    self.moveUp = True                    
                if event.key == K_DOWN or event.key == ord('s'):
                    self.moveUp = False
                    self.moveDown = True
            if event.type == KEYUP:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
                if event.key == K_LEFT or event.key == ord('a'):
                    self.moveLeft = False
                if event.key == K_RIGHT or event.key == ord('d'):
                    self.moveRight = False
                if event.key == K_UP or event.key == ord('w'):
                    self.moveUp = False
                if event.key == K_DOWN or event.key == ord('s'):
                    self.moveDown = False

    def move(self):
        self.GetEvent()
        if(self.moveRight is True):
            
            self.PlayerRect.right = self.PlayerRect.right+ 5
            
            self.ImageCount = self.ImageCount + 1            
            if(self.ImageCount == 2):
                self.CurrentHeroImage = self.PlayerImage[self.MoveImageCount]
                self.ImageCount = 0
                
                self.MoveImageCount = self.MoveImageCount + 1
            
            if(self.MoveImageCount > 3):
                 self.MoveImageCount = 0
    
        if(self.moveLeft is True):
            self.PlayerRect.left = self.PlayerRect.left - 5
            self.ImageCount = self.ImageCount + 1            
            if(self.ImageCount == 2):
                self.CurrentHeroImage = self.PlayerImage[self.MoveImageCount]
                self.ImageCount = 0
                
                self.MoveImageCount = self.MoveImageCount + 1
            
            if(self.MoveImageCount > 3):
                 self.MoveImageCount = 0
        if(self.moveUp is True):                      
            self.Jumped = True          
        
        if(self.Jumped is True):
            self.jump()
            
    def UpdateHeroImageOnScreen(self):
        self.screen.BlitToSurface(self.CurrentHeroImage , self.PlayerRect)
        
    def jump(self):
        #jumpcount attempts to simulate gravity- move Mario up 10 times, then down 10 times
        if(self.JumpCount < 10):
            self.PlayerRect.top = self.PlayerRect.top - 5  
            self.JumpCount = self.JumpCount + 1
            self.CurrentHeroImage = self.PlayerImage[4]  
        elif(self.JumpCount >= 10):    
            self.PlayerRect.top = self.PlayerRect.top + 5  
            self.JumpCount = self.JumpCount + 1 
            self.CurrentHeroImage = self.PlayerImage[4]  
        
        if(self.JumpCount == 20):
            self.JumpCount = 0
            self.Jumped = False
            self.CurrentHeroImage = self.PlayerImage[0]  
        
    def attack():
        pass
    

